<?php

namespace App\Admin\Controllers;

use App\Admin\Contracts\ActionInterface;
use App\Admin\Contracts\AuthRuleInterface;

use App\Admin\Contracts\MemberLevelInterface;
use App\Admin\Contracts\MenuInterface;
use App\Admin\Models\AuthRuleModel;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Route;


/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/2/6 0006
 * Time: 11:12
 */
class IndexController extends Controller
{
    /**
     * @var AuthRuleInterface
     */
    public $iAuthRule;
    /**
     * 菜单接口
     * @var MenuInterface
     */
    private $iMenu;

    /**
     * 构造函数
     * IndexController constructor.
     */
    public function __construct()
    {
        $this->iAuthRule = app()->make(AuthRuleInterface::class);
        $this->iMenu = app()->make(MenuInterface::class);
    }

    /**
     * 行为操作表
     * @return ActionInterface
     */
    public function getActionInterface(){
        return app()->make(ActionInterface::class);
    }


    /**
     * 层级统计列表
     * @return MemberLevelInterface
     */
    public function getMemberLevelInterface(){
        return app()->make(MemberLevelInterface::class);
    }

    /**
     * 首页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        //拉取获取菜单
        $menus = $this->getMenus();
        return view("admin.index")->with(["menu" => $menus]);
    }

    /**
     * 控制页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function console()
    {
        $list=$this->getMemberLevelInterface()->getList([]);
        return view("admin.home.console")->with("list",$list);
    }

    /**
     * 用户登陆日志
     */
    public function userLoginLog(Request $request){
         $returnlist=$this->getActionInterface()->getActionLogByActionName("user_login");
         return $returnlist;
    }

    /**
     * 获取控制器菜单数组,二级菜单元素位于一级菜单的'child'元素中
     * @author 林群伟  <278805354@qq.com>
     */
    public function getMenus()
    {

        $where['pid'] = 0;
        $where['hide'] = 0;
        if (!config('adminconfig.DEVELOP_MODE')) { // 是否开发者模式
            $where['is_dev'] = 0;
        }

        $menus['main'] = $this->iMenu->getMenuList($where)->toArray();   //先获取顶级

        foreach ($menus['main'] as $keys => $item) {
            $where = array();
            $where['pid'] = $item['id'];
            $where['hide'] = 0;
            if (!config('lzy.DEVELOP_MODE')) { // 是否开发者模式
                $where['is_dev'] = 0;
            }
            $route = Route::current();
            $rmodel = $route->action["prefix"];
            if (!empty($rmodel)) {
                $rmodel = substr($rmodel, 1);
            }

            if (stripos($item['url'], $rmodel) !== 0) {
                $item['url'] = $rmodel . '/' . $item['url'];
            }

            //判断主菜单权限
            if (!IS_ROOT && !$this->checkRule($item['url'], AuthRuleModel::RULE_MAIN, null)) {


                unset($menus['main'][$keys]);
                continue; //继续循环
            }


            $second_urls = $this->iMenu->getMenuList($where)->toArray();  //获取二级菜单

            foreach ($second_urls as $k => $v) {

                $second_url = $rmodel . '/' . $v['url'];

                if (!IS_ROOT && !$this->checkRule($second_url, AuthRuleModel::RULE_URL, null)) {

                } else {

                    $g = $v['group'];
                    $menus['main'][$keys]['child'][$g][] = $v;
                }
            }
        }

        return $menus;
    }

    /**
     * 权限检测
     * @param string $rule 检测的规则
     * @param string $mode check模式
     * @return boolean
     * @author 朱亚杰  <xcoolcc@gmail.com>
     */
    final protected function checkRule($rule, $type = AuthRuleModel::RULE_URL, $mode = 'url')
    {
        static $Auth = null;
        if (!$Auth) {

            $Auth = new \App\Base\Helpers\Lib\Auth();

        }

        if (!$Auth->check($rule, UID, $type, $mode)) {
            return false;
        }
        return true;
    }
}